// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
import store from './store'
import qs from 'qs'
import Cookies from 'js-cookie'; 
Vue.use(ElementUI)

Vue.config.productionTip = false
Vue.prototype.$axios=axios

// 创建一个 axios 实例
const service = axios.create({
  baseURL: 'http://localhost:8889', // 你的 API 基础 URL
  timeout: 5000 // 请求超时时间
});
// main.js
console.log('Store state:', store.state); // 调试信息

// 请求拦截器
service.interceptors.request.use(
  config => {
    // const token = store.state.token; // 从 Vuex Store 获取 token
    const token = Cookies.get('token');
    if (token) {
      config.headers['token'] = `${token}`; // 设置请求头
    }
    return config;
  },
  error => {
    console.log(error);
    Promise.reject(error);
  }
);

// 响应拦截器
service.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    console.log('err' + error);
    return Promise.reject(error);
  }
);

Vue.prototype.$axios = service;

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  // axios,
  store,
  components: { App },
  template: '<App/>'
})


axios.interceptors.request.use((config) => {
    if(config.method === 'post'){
      config.paramsSerializer = function(params){
      return qs.stringify(params,{arrayFormat: "repeat"})
      } 
    }  
    return config
  },function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});
